package com.mooer.app;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.mooer.R;
import com.mooer.bean.Shop;
import com.mooer.utils.ui.UiUtils;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-12 上午07:32:17 类说明
 */
public class Business extends TabActivity implements OnClickListener {

	private TabHost tabHost;

	private TabWidget tabWidget;

	private ImageView info, gallery, comment, signin;

	private List<View> vs;
	private OnPreDrawListener op;

	public static Shop businessInfo;

	private String tab;

	public static Business instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.business);
		Intent intent = getIntent();
		tab = intent.getStringExtra("tab");
		businessInfo = (Shop) intent.getSerializableExtra("businessInfo");
		vs = new ArrayList<View>();
		info = (ImageView) findViewById(R.id.business_info);
		gallery = (ImageView) findViewById(R.id.business_gallery);
		comment = (ImageView) findViewById(R.id.business_dianpin);
		signin = (ImageView) findViewById(R.id.business_signin);
		vs.add(info);
		vs.add(gallery);
		vs.add(comment);
		vs.add(signin);
		op = UiUtils.initButtonState(info);
		initTab();
		instance = this;
	}

	private void initTab() {
		tabHost = getTabHost();
		Intent layout1intent = new Intent();
		layout1intent.setClass(this, BusinessInfo.class);
		layout1intent.putExtra("businessInfo", businessInfo);
		final TabHost.TabSpec layout1spec = tabHost.newTabSpec("info");
		layout1spec.setIndicator("");
		layout1spec.setContent(layout1intent);
		tabHost.addTab(layout1spec);

		Intent layout2intent = new Intent();
		layout2intent.setClass(this, BusinessGallery.class);
		final TabHost.TabSpec layout2spec = tabHost.newTabSpec("gallery");
		layout2intent.putExtra("shopId", businessInfo.getId());
		layout2spec.setIndicator("");
		layout2spec.setContent(layout2intent);
		tabHost.addTab(layout2spec);

		Intent layout3intent = new Intent();
		layout3intent.putExtra("shopId", businessInfo.getId());
		layout3intent.setClass(this, BusinessComment.class);
		final TabHost.TabSpec layout3spec = tabHost.newTabSpec("comment");
		layout3spec.setIndicator("");
		layout3spec.setContent(layout3intent);
		tabHost.addTab(layout3spec);

		Intent layout4intent = new Intent();
		layout4intent.putExtra("businessInfo", businessInfo);
		layout4intent.setClass(this, BusinessSignin.class);
		final TabHost.TabSpec layout4spec = tabHost.newTabSpec("signin");
		layout4spec.setIndicator("");
		layout4spec.setContent(layout4intent);
		tabHost.addTab(layout4spec);

		tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			View view = tabWidget.getChildAt(i);
			view.setBackgroundColor(0);
		}
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				if ("info".equals(tabId)) {
					UiUtils.buttonStateChange(vs, info, op);
				} else if ("gallery".equals(tabId)) {
					UiUtils.buttonStateChange(vs, gallery, op);
				} else if ("comment".equals(tabId)) {
					UiUtils.buttonStateChange(vs, comment, op);
				} else if ("signin".equals(tabId)) {
					UiUtils.buttonStateChange(vs, signin, op);
				}

			}
		});
		if ("sign".equals(tab)) {
			tabHost.setCurrentTabByTag("signin");
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void switchTab(String tab) {
		tabHost.setCurrentTabByTag(tab);
	}

}
